package com.situ.gym.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.gym.pojo.Permission;
import com.situ.gym.mapper.PermissionMapper;
import com.situ.gym.pojo.vo.PermissionVO;
import com.situ.gym.pojo.vo.RouterVO;
import com.situ.gym.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2025-09-03
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    //1.使用Map实现
    //缺点：1、在内存中，重启就会销毁 2、只能在单机模式下有效，无法实现分布式环境缓存
    private Map<String,Object>map = new HashMap<String,Object>();

    @Override
    public void update(Permission permission) {
        permissionMapper.updateById(permission);
        redisTemplate.delete("permissionVOTree");
    }

    @Override
    public List<PermissionVO> selectPermissionTree() {
        //先检查redis缓存中有没有数据
        List<PermissionVO>permissionVOTree = redisTemplate.opsForList().range("permissionVOTree", 0, -1);
        //redis中如果没有数据
        if (CollectionUtils.isEmpty(permissionVOTree)) {
            //查找所有的权限
            QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByAsc("sort");
            List<Permission> permissionList = permissionMapper.selectList(queryWrapper);
            List<PermissionVO> permissionVOList = new ArrayList<>();
            permissionList.forEach(permission -> {
                PermissionVO permissionVO = new PermissionVO();
                BeanUtils.copyProperties(permission, permissionVO);
                permissionVOList.add(permissionVO);
            });

            permissionVOTree = buildTree(permissionVOList);
            //更新到redis中
            redisTemplate.opsForList().rightPushAll("permissionVOTree", permissionVOTree);
        }
        return permissionVOTree;
    }


    //@Override
    public List<PermissionVO> selectPermissionTree1() {
        //先检查map缓存中有没有数据
        List<PermissionVO>permissionVOTree = (List<PermissionVO>) map.get("permissionVOTree");
        //map缓存里面如果没有数据
        if(CollectionUtils.isEmpty(permissionVOTree)){
            //查询数据库
            //查找所有的权限
            QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByAsc("sort");
            List<Permission> permissionList = permissionMapper.selectList(queryWrapper);
            List<PermissionVO> permissionVOList = new ArrayList<>();
            permissionList.forEach(permission -> {
                PermissionVO permissionVO = new PermissionVO();
                BeanUtils.copyProperties(permission, permissionVO);
                permissionVOList.add(permissionVO);
            });

            permissionVOTree = buildTree(permissionVOList);

            //更新到map缓存中
            map.put("permissionVOTree",permissionVOTree);
        }
        return permissionVOTree;
    }

    @Override
    public List<PermissionVO> selectRouterTreeByAdminId(Integer id) {
        //1.查找当前用户的所有权限
        List<Permission>permissionList = permissionMapper.selectPermissionByAdminId(id);
        //2.将List<Permission>转化成List<PermissionVO>
        List<PermissionVO> permissionVOList = new ArrayList<>();
        permissionList.forEach(permission -> {
            PermissionVO permissionVO = new PermissionVO();
            BeanUtils.copyProperties(permission, permissionVO);
            permissionVOList.add(permissionVO);
        });
        //3.构建树形结构
        List<PermissionVO> permissionVOTree = buildTree(permissionVOList);
        //4.只保留路由
        //List<RouterVO>routerTreeList = buildRouterTree(permissionVOTree);
        return permissionVOTree;
    }

   /* private List<RouterVO> buildRouterTree(List<PermissionVO> permissionVOSTree) {
        List<RouterVO> routerTreeList = new ArrayList<>();
        //首先遍历一级目录
        for (PermissionVO permissionVO : permissionVOSTree) {
            RouterVO routerVO = new RouterVO();
        }
    }*/

    @Override
    public List<String> selectBtnListByAdminId(Integer id) {
        List<Permission>permissionList = permissionMapper.selectPermissionByAdminId(id);
        List<String> btnList = new ArrayList<>();
        for (Permission permission : permissionList) {
            if (permission.getType() == 2)
                btnList.add(permission.getPermissionValue());
        }
        return btnList;
    }

    private List<PermissionVO> buildTree(List<PermissionVO> permissionVOList) {
        List<PermissionVO> treeList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(permissionVOList)){
            for (PermissionVO permissionVO : permissionVOList) {
                if (permissionVO.getParentId() == 0){
                    permissionVO.setChildren(buildChildTree(permissionVO,permissionVOList));
                    treeList.add(permissionVO);
                }
            }
        }

        return treeList;
    }

    private List<PermissionVO> buildChildTree(PermissionVO permissionVO, List<PermissionVO> permissionVOList) {
        List<PermissionVO> childTree = new ArrayList<>();
        if (!CollectionUtils.isEmpty(permissionVOList)){
            for (PermissionVO permission : permissionVOList) {
                if (permission.getParentId().longValue() == permissionVO.getId().longValue()){
                    //添加孩子的时候，首先把这个孩子的集合查出来
                    permission.setChildren(buildChildTree(permission,permissionVOList));
                    childTree.add(permission);
                }
            }
        }
        return childTree;
    }
}
